﻿var lay;
var element;
var form;
var util;
var layer;
var oList = [];
/** * */
$(function() {
	layui.use(['element','form','util','layer'], function(){
		element = layui.element;
		form=layui.form;
		util = layui.util;
		layer = layui.layer;
		//执行
		util.fixbar({
			bgcolor:"rgba(255,255,255,.08);",
		    click: function(type){
		      console.log(type);
		    }
		});
		// 设备
		selectMachine();
		//setInterval("selectMachine()", 1000*60*2*2);
		// 业主职工数
		selectTenantCount();
		setInterval("selectTenantCount()", 1000*60*5);
		// 刷脸信息
		selectLastScan();
		// 识别人数
		selectFaceCount();
		// 实时人流
		selectDay24();
	})
	// 根据设备查询
	$("#selectSB").click(function(){
		lay = showLoad("切换中",0.5);
		$.ajaxSettings.async = false;
		mIds = "";
		lastTime = "";
		img6Count = 0;
		$("#last_per").empty();
		var check = $("input[name='m']");
		for(var i=0; i<check.length; i++){
			if(check[i].checked){
				var value = check[i].value;
				if(mIds == ""){
					mIds = value;
				}else{
					mIds += ","+value;
				}
			}
		}
		// 刷脸信息
		selectLastScan();
		// 识别人数
		selectFaceCount();
		// 实时人流
		selectDay24();
		$.ajaxSettings.async = true;
	})
});

/**
 * 业主-职工人数
 * @returns
 */
function selectTenantCount(){
	$.post("data/selectTenantCount", {'oid': oid}, function (data) {
        if(data.success){
        	var map = data.data;
        	var tList = map.tList;
        	var pList = map.pList;
        	var sexVal1 = 0;
        	var sexVal2 = 0;
        	if(tList != null && tList.length > 0){
        		for(var i=0; i<tList.length; i++){
        			var obj = tList[i]; 
        			$("#count_yz"+obj.num).text(parseFloat(obj.count).toLocaleString());
        			if(obj.num == 3){
        				sexVal1 += obj.count;
        			}else if(obj.num == 4){
        				sexVal2 += obj.count;
        			}
        		}
        	}
        	if(pList != null && pList.length > 0){
        		for(var i=0; i<pList.length; i++){
        			var obj = pList[i]; 
        			$("#count_zg"+obj.num).text(parseFloat(obj.count).toLocaleString());
        			if(obj.num == 3){
        				sexVal1 += obj.count;
        			}else if(obj.num == 4){
        				sexVal2 += obj.count;
        			}
        		}
        	}
        	setEcharts2(sexVal1, sexVal1+sexVal2);
        	setEcharts3(sexVal2, sexVal1+sexVal2);
        }
    })
}
/**
 * 实时流量
 * @returns
 */
function selectDay24(){
	$.post("data/selectDay24", {'ids':mIds, 'oid': oid}, function (data) {
        if(data.success){
        	var list = data.list;
        	if(list != null && list.length > 0){
        		setEcharts1(list);
        		layer.close(lay);
        	}
        }
    })
}
/**
 * 今日识别数
 * @returns
 */
function selectFaceCount(){
	$.post("data/selectFaceCount", {'ids':mIds, 'oid': oid}, function (data) {
		if(data.success){
			var list = data.list;
			if(list != null && list.length > 0){
				for(var i=0; i<list.length;i++){
					var obj = list[i];
					if(obj.num == 1){
						$("#zh_count").html(getNum(obj.count));
					}else if(obj.num == 2){
						$("#zg_count").html(getNum(obj.count));
					}else if(obj.num == 3){
						$("#ms_count").html(getNum(obj.count));
					}
				}
			}
		}
	})
}
function getNum(value){
	var a='';
	var temp = ""+(parseFloat(value).toLocaleString());
	for(var i=0; i<temp.length; i++){
		var v = temp.charAt(0);
		a += '<span style="background: #4f83d2;padding: 0px 6px;margin-right: 5px;">'+v+'</span>';
	}
	a += '<span style="font-size:22px;">人</span>';
	return a;
}

var mIds = "";
var lastTime = "";
var img6Count = 0;
/**
 * 最近刷脸
 * @returns
 */
function selectLastScan(){
	$.post("data/selectLastScan", {'ids':mIds, 'lastTime':lastTime, 'oid': oid}, function (data) {
        if(data.success){
        	var list = data.list;
        	//$("#last_per").empty();
        	if(list != null && list.length > 0){
        		for(var i=0; i<list.length;i++){
        			var obj = list[i];
        			if(lastTime && img6Count > 5){
						$("#last_per>.scan_box:eq(0)").remove();
					}
    				var s = '';
    				s += '<div class="scan_box" style="width:19.9%;height:100%;float: left;position: relative;border-right: 1px solid #093d56;">';
    				s += '	<div style="width:50%;height:100%;position: relative;top: 0%;float: left;border-radius: 5px;">';
    				s += '	  <img style="width:94%;height:48%;margin-top: 2%;margin-left: 4%;float: left;border-radius: 5px;" src="'+systemPath+obj.jzjz+'?v='+Math.random()+'" onerror="javascript:this.src=\''+"images/head.png"+'\'">';
    				s += '	  <img style="width:94%;height:48%;margin-top: 2%;margin-left: 4%;float: left;border-radius: 5px;" src="'+obj.successImage+'" onerror="javascript:this.src=\''+"images/head.png"+'\'">';
    				s += '	</div>';
    				s += '	<div style="width:49%;height:95%;position: relative;top: 5%;float: left;left: 1%;">';
    				s += '		<div style="width:100%;position: relative;height: 20%;float: left;white-space:nowrap;">';
    				s += '			<span style="width:40%;">姓名：</span>';
    				s += '			<span style="width:60%;">'+obj.name+'</span>';
    				s += '		</div>';
    				s += '		<div style="width:100%;position: relative;height: 20%;float: left;white-space:nowrap;">';
    				s += '			<span style="width:40%;">性别：</span>';
    				var sex = ' <span class="sb_zt" style="background: #eaeaec;color: #101010;">无</span>';
    				if(obj.sex == '男'){
    					sex = '<span class="sb_zt" style="background: #0798af;">男</span>';
    				}else if(obj.sex == '女'){
    					sex = '<span class="sb_zt" style="background: #c32c79;">女</span>';
    				}
    				var arr = obj.scantime.split(" ");
    				s += sex;
    				s += '		</div>';
    				s += '		<div style="width:100%;position: relative;height: 20%;float: left;white-space:nowrap;">';
    				s += '			<span style="width:40%;">角色：</span>';
    				s += '			<span style="width:60%;">'+obj.rname+'</span>';
    				s += '		</div>';
    				s += '		<div style="width:100%;position: relative;height: 20%;float: left;white-space:nowrap;">';
    				s += '			<span style="width:40%;">日期：</span>';
    				s += '			<span style="width:60%;">'+arr[0]+'</span>';
    				s += '		</div>';
    				s += '		<div style="width:100%;position: relative;height: 20%;float: left;white-space:nowrap;">';
    				s += '			<span style="width:40%;">时间：</span>';
    				s += '			<span style="width:60%;">'+arr[1]+'</span>';
    				s += '		</div>';
    				s += '	</div>';
    				s += '</div>';
    				$("#last_per").append(s);
    				if(img6Count < 6){
    					if(img6Count > 5){
    						img6Count = 6;
    					}else{
    						img6Count ++;
    						if(img6Count >= 5){
    							img6Count = 6;
    						}
    					}
    				}
        		}
        		var obj = list[list.length - 1];
        		lastTime = obj.scantime;
        	}
        }
    })
}
/**
 * 设备-项目名称
 * @returns
 */
function selectMachine(){
	$.post("data/selectMachine", {'oid': oid}, function (data) {
        if(data.success){
        	var list = data.list;
        	var oname = data.data;
        	//$(".head_text").text(oname);
        	$("#sb_all").empty();
        	if(list != null && list.length > 0){
        		for(var i=0; i<list.length ;i++){
        			var obj = list[i];
        			var s = '';
        			s+= '<div class="sb_box">';
        			s+= '	<span class="sb_lxh">'+(i+1)+'</span>';
        			s+= '	<span class="sb_fx layui-form">';
        			s+= '		<span style="position: relative;top: 10px;"><input type="checkbox" name="m" value="'+obj.macno+'" class="check" lay-skin="primary" title="" checked=""></span>';
        			s+= '	</span>';
        			s+= '	<span class="sb_ip">'+obj.ipAddr+'</span>';
        			s+= '	<span class="">';
        			var zx = '';
        			if(obj.state == 1){
        				zx = '<span class="sb_zt" style="background: #0a9622;">在线</span>';
        			}else{
        				zx = '<span class="sb_zt" style="background: #868788;">离线</span>';
        			}
        			s+=zx;
        			s+= '	</span>';
        			s+= '</div>';
        			$("#sb_all").append(s);
        		}
        	}
        	form.render("checkbox");
        }
    })
}

/**
 * 打开加载中
 * @param title
 * @returns
 */
function showLoad(title,shade) {
    var s = '<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="color: white;font-size: 30px;">&#xe63d;</i>';
    var lay = layer.msg(s + "<span style='position: relative;top: -2px;left: 5px;'>" + title + "...</span>", {
        shade: shade?shade:false,
        time: 0,
        zIndex:999999999999
    });
    return lay;
}
/**
 * 当前时间
 * @returns
 */
function getSysDate(){
	var time = new Date();
	var month = ((time.getMonth()+1)+"").length < 2?"0"+(time.getMonth()+1):(time.getMonth()+1);
	var dates = (time.getDate()+"").length < 2?"0"+time.getDate():time.getDate();
	var minute = (time.getMinutes()+"").length < 2?"0"+time.getMinutes():time.getMinutes();
	var hours = (time.getHours()+"").length < 2?"0"+time.getHours():time.getHours();
	var minute = (time.getMinutes()+"").length < 2?"0"+time.getMinutes():time.getMinutes();
	var second = (time.getSeconds()+"").length < 2?"0"+time.getSeconds():time.getSeconds();
	var s = time.getFullYear()+"-"+month+"-"+dates+" "+hours+":"+minute+":"+second+" "+getWeek(time);
	$(".date_div").text(s);
}
function getWeek(date){
	var week = date.getDay();  
	var str = "";
	if (week == 0) {  
	        str = "星期日";  
	} else if (week == 1) {  
	        str = "星期一";  
	} else if (week == 2) {  
	        str = "星期二";  
	} else if (week == 3) {  
	        str = "星期三";  
	} else if (week == 4) {  
	        str = "星期四";  
	} else if (week == 5) {  
	        str = "星期五";  
	} else if (week == 6) {  
	        str = "星期六";  
	} 
	return str;
}
getSysDate();
setInterval("getSysDate()",1000);
